package cn.lixu.org.controller;

import cn.lixu.basic.exception.BusinessException;
import cn.lixu.basic.util.ExcelUtils;
import cn.lixu.basic.util.PageList;
import cn.lixu.basic.util.JsonResult;
import cn.lixu.org.domain.Shop;
import cn.lixu.org.domain.ShopLog;
import cn.lixu.org.dto.ShopDto;
import cn.lixu.org.query.ShopQuery;
import cn.lixu.org.service.IShopService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService ShopService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Shop> loadAll() {
        return ShopService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id") Long id) {
        return ShopService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            ShopService.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param Shop
     * @return
     */
    @PutMapping
    public JsonResult addAndUpdate(@RequestBody Shop Shop) {
        try {
            if (Shop.getId() == null) {
                ShopService.save(Shop);
            } else {
                ShopService.update(Shop);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param ShopQuery
     * @return
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery ShopQuery) {
        return ShopService.queryPage(ShopQuery);

    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            ShopService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }

    @ApiOperation(value = "商家入驻", notes = "")
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop) {
        try {
            ShopService.settlement(shop);
            return JsonResult.me();
        } catch (BusinessException e) {
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败，请稍候再试");
        }
    }

    /**
     * 审核通过
     *
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过", notes = "")
    public JsonResult pass(@RequestBody ShopLog log) {
        try {
            ShopService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }
    }

    /**
     * 审核驳回
     *
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "审核驳回", notes = "")
    public JsonResult reject(@RequestBody ShopLog log) {
        try {
            ShopService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核被驳回，请再次提交数据");
        }
    }


    /**
     * Excel文件导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            //查询全部的shop数据
            List<Shop> list = ShopService.loadAll();
            //调用工具类
            //List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response
            //Class<?> pojoClass类似于domain.class
            ExcelUtils.exportExcel(list, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件导入
     * RequestPart  文件上传必须使用这个注解
     *
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart(value = "file", required = true) MultipartFile file) {
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入

        //保存到数据库中[自己实现，提示：动态sql 批量插入]
        list.forEach(System.out::println);
    }

    @GetMapping("/getEcharts")
    public List<ShopDto> getEcharts(){
        return ShopService.getEcharts();
    }

}

